package com.winhxd.b2c.common.feign.customer;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.customer.condition.CopyPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.condition.CustomerPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.condition.QueryPageConfigCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerPageConfig;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @program: hxd-outlet-parent
 * @description: 页面配置client
 * @author: ChenYanqi
 * @create: 2019-01-11 18:04
 **/
@FeignClient(value = ServiceName.CUSTOMER_SERVICE, fallbackFactory = com.winhxd.b2c.common.feign.customer.PageConfigServiceClientFallBack.class)
public interface PageConfigServiceClient {

    /**
     * 保存页面配置
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2400/v1/savePageConfig", method = RequestMethod.POST)
    ResponseResult<Boolean> savePageConfig(@RequestBody CustomerPageConfigCondition condition);

    /**
     * 修改页面配置
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2401/v1/modifyPageConfig", method = RequestMethod.POST)
    ResponseResult<Boolean> modifyPageConfig(@RequestBody CustomerPageConfigCondition condition);

    /**
     * 删除页面配置
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2402/v1/deletePageConfig", method = RequestMethod.POST)
    ResponseResult<Boolean> deletePageConfig(@RequestBody QueryPageConfigCondition condition);

    /**
     * 查询页面配置列表
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2403/v1/queryPageConfig", method = RequestMethod.POST)
    ResponseResult<PagedList<CustomerPageConfig>> queryPageConfig(@RequestBody QueryPageConfigCondition condition);

    /**
     * 发布页面
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2404/v1/releasePageConfig", method = RequestMethod.POST)
    ResponseResult<Boolean> releasePageConfig(@RequestBody QueryPageConfigCondition condition);

    /**
     * 设为主页
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2405/v1/setHomePage", method = RequestMethod.POST)
    ResponseResult<Boolean> setHomePage(@RequestBody QueryPageConfigCondition condition);

    /**
     * 复制页面
     *
     * @param condition
     * @return
     */
    @RequestMapping(value = "/store/2406/v1/copyPageConfig", method = RequestMethod.POST)
    ResponseResult<Boolean> copyPageConfig(@RequestBody CopyPageConfigCondition condition);

    /**
     * 根据页面id获取页面详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/store/2407/v1/getPageConfigById", method = RequestMethod.POST)
    ResponseResult<CustomerPageConfig> getPageConfigById(@RequestParam("id") Long id);


}

@Component
class PageConfigServiceClientFallBack implements PageConfigServiceClient, FallbackFactory<PageConfigServiceClient> {

    Logger logger = LoggerFactory.getLogger(PageConfigServiceClientFallBack.class);
    Throwable throwable;

    @Override
    public ResponseResult<Boolean> savePageConfig(CustomerPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> savePageConfig{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> modifyPageConfig(CustomerPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> modifyPageConfig{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> deletePageConfig(QueryPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> saveDeliveryInfo{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<PagedList<CustomerPageConfig>> queryPageConfig(QueryPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> queryPageConfig{}", throwable.getMessage());
        return new ResponseResult<PagedList<CustomerPageConfig>>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> releasePageConfig(QueryPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> releasePageConfig{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> setHomePage(QueryPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> setHomePage{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> copyPageConfig(CopyPageConfigCondition condition) {
        logger.error("PageConfigServiceClientFallBack -> copyPageConfig{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<CustomerPageConfig> getPageConfigById(Long id) {
        logger.error("PageConfigServiceClientFallBack -> getPageConfigById{}", throwable.getMessage());
        return new ResponseResult<CustomerPageConfig>(BusinessCode.CODE_1001);
    }

    @Override
    public PageConfigServiceClient create(Throwable throwable) {
        this.throwable = throwable;
        return new PageConfigServiceClientFallBack();
    }
}